package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import database.UserOperations;

@SuppressWarnings("serial")
public class adminPopupGUI extends JFrame implements ActionListener 
{
	private JButton login, cancel;
	private JPanel p1, p2;
	private JLabel l1,l2;
	private JTextField f1,f2;
	private UserOperations uo;

	public adminPopupGUI() 
	{
		super("Admin Login");
		this.setSize(300, 120);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		uo=new UserOperations();
		
		//panel
		p1 = new JPanel(new GridLayout(2, 2));
		l1 = new JLabel("Username:");
		l2 = new JLabel("Password:");
		f1 = new JTextField(10);
		f2 = new JTextField(10);
		
		p1.add(l1);
		p1.add(f1);
		p1.add(l2);
		p1.add(f2);
		
		//panel 2
		p2 = new JPanel();
		cancel = new JButton("CANCEL");
		cancel.addActionListener(this);
		login = new JButton("LOGIN");
		login.addActionListener(this);
		
		p2.add(cancel);
		p2.add(Box.createRigidArea(new Dimension(50,0)));
		p2.add(login);
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.SOUTH);
	}

	//check if admin
	public boolean check()
	{
		boolean check=false;
		String[] list=new String[2];
		list=uo.admin();
		System.out.println(list[0]);
		System.out.println(list[1]);
		if(list[0].equals(f1.getText()) && list[1].equals(f2.getText()))
		{
			check=true;
		}
		return check;
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==cancel)
		{
			this.setVisible(false);
		}
		if(ae.getSource()==login)
		{
			if(check()==true)
			{
				this.setVisible(false);
				AddNewUserSecurity anu=new AddNewUserSecurity();
				anu.setVisible(true);
			}
			else
			{
				this.setVisible(false);
				JOptionPane.showMessageDialog(null, "ACCESS DENIED - you must be the administrator to perform this task!"
						,"ERROR",JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	public static void main(String args[]) 
	{
	
		adminPopupGUI ad = new adminPopupGUI();
		ad.setVisible(true);
	}

}
